package main

import (
	"encoding/gob"
	"fmt"
	"os"
)

// gob编码到文件

type Address struct {
	Type, City, Country string
}

type VCard struct {
	Name      string
	Addresses []*Address
	Remark    string
}

func main() {
	pa := &Address{"pravite", "Shanghai", "China"}
	wa := &Address{"work", "Beijing", "China"}
	vc := VCard{"Hanhua", []*Address{pa, wa}, "none"}
	file, err := os.OpenFile("vcard.gob", os.O_WRONLY|os.O_CREATE, 0666)
	if err != nil {
		fmt.Fprintf(os.Stderr, "gob: opening file error: %s\n", err)
		os.Exit(1)
	}
	defer file.Close()
	enc := gob.NewEncoder(file)
	err = enc.Encode(vc)
	if err != nil {
		fmt.Fprintf(os.Stderr, "gob: encoding error: %s\n", err)
		os.Exit(1)
	}
	fmt.Println("encode vcard to vcard.gob done")
}
